<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing permissions.
 * @package backend-permissions/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Permission extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();

        $this->load->model('permission_model');
        $this->load->model('backend-modules/module_model');
        $this->load->model('backend-roles/role_model');
        $this->load->model('backend-companies/company_model');
        $this->load->model('backend-companies/company_modules_model');
        $this->load->model('backend-companies/company_roles_model');
        
        $this->load->helper('cms');
        $this->load->helper('permission');
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');
        
        init_permission();
    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        view_all_permissions();
//        view_permissions_list();
    }
    
    /**
     * Update Permissions
     * @access public
     */
    public function edit()
    {
        
//        echo '<pre>';
//        print_r($_POST); 
//        echo '</pre>';
//        exit();
        
        if( isset($_POST) && !empty($_POST) )
        {
            $company_id     = $this->input->post('company_id');
            $group_id       = $this->input->post('group_id');
            $permissions    = $this->input->post('permissions');
            
            if( $group_id )
            {
                $args = array('company_id' => $company_id, 'group_id' => $group_id);
                $this->permission_model->delete($args);
                
                if( $permissions )
                {
                    foreach($permissions as $key => $permission)
                    {
                        $ids            = explode('-', $key);
                        $company_id     = isset( $ids[0] ) ? $ids[0] : NULL;
                        $group_id       = isset( $ids[1] ) ? $ids[1] : NULL;
                        $module_id      = isset( $ids[2] ) ? $ids[2] : NULL;
                        $args           = array('company_id' => $company_id, 'group_id' => $group_id, 'module_id' => $module_id); 

                        $permission_set                         = array();
                        $permission_set['company_id']           = $company_id;
                        $permission_set['group_id']             = $group_id;
                        $permission_set['module_id']            = $module_id;
                        $permission_set['group_permissions']    = serialize($permission);

                        $this->permission_model->update_permission($args, $permission_set );
                    }
                }
                
                view_all_permissions('edit_success');
                
            } else
            {
                view_all_permissions('invalid_parameters');
            }
            
        } else
        {
            $this->show_404();
        }
    }
}

/* End of file permission.php */
/* Location: ./application/permissions/backend-permissions/controllers/permission.php */